﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db4objects.Db4o;

namespace ADB_QuanLyThuVien
{
    public class DAO_Authors
    {
        public static bool AddAuthor(Author tg)
        {
            bool kq = true;
            if (isExist(tg) == true)
                kq = false;
            else
            {
                IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
                tg.Id = GetNextId();
                db.Store(tg);
                db.Close();
            }
            
            return kq;
        }

        private static string GetNextId()
        {
            string nextid = "AU";
            DateTime now = DateTime.Now;
            nextid += now.Year.ToString("0000") + now.Month.ToString("00") + now.Day.ToString("00") + now.Hour.ToString("00") + now.Minute.ToString("00") + now.Second.ToString("00");
            return nextid;
        }

        private static bool isExist(Author tg)
        {
            bool kq = true;
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<Author> list = db.Query(delegate(Author au) // câu truy vấn theo nagative query
            {
                if (au.Id.Contains("AU") && au.Name == tg.Name && au.ButDanh == tg.ButDanh && au.TrangWebCaNhan == tg.TrangWebCaNhan && au.Birthday == tg.Birthday && au.Address == tg.Address && au.PhoneNumber == tg.PhoneNumber && au.Email == tg.Email)
                    return true;
                return false;
            }
            );
            if (list.Count == 0)
                kq = false;
            db.Close();
            return kq;
        }

        public static List<Author> SelectAll()
        {
            List<Author> list = new List<Author>();
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            Author tg = new Author();
            IList<Author> result = db.Query(delegate(Author au) // câu truy vấn theo nagative query
            {
                if (au.Id.Contains("AU"))
                    return true;
                return false;
            }
             );
            for (int i = 0; i < result.Count; ++i)
                list.Add((Author)result[i]);
            db.Close();
            return list;
        }

        public static bool RemoveAuthor(string idAuthor)
        {
            bool kq = true;
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            Author tg = new Author();
            tg.Id = idAuthor;
            IObjectSet result = db.QueryByExample(tg);
            db.Delete((Author)result[0]);
            db.Close();
            return kq;
        }

        internal static Author GetAuthor(string idAuthor)
        {
            Author tg = new Author();
            tg.Id = idAuthor;
            Author kq;
            using (IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString))
            {
                IObjectSet result = db.QueryByExample(tg);
                kq = (Author)result[0];
            }
            return kq;
        }

        internal static Author GetAuthorByName(string auname)
        {
            Author tg = new Author();
            tg.Name = auname;
            Author kq;
            using (IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString))
            {
                IObjectSet result = db.QueryByExample(tg);
                kq = (Author)result[0];
            }
            return kq;
        }
    }
}
